<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul>
        <li><a href="/home">首页</a></li>
        <li><a href="/about">关于</a></li>
    </ul>

    <!-- 当 url 变更后，展示对应的代码片段 -->
    <div id="root"></div>

    <script>
        const routes = [
            {
                path: '/home',
                component: () => {
                    return '<h1>首页页面</h1>'
                }
            },
            {
                path: '/about',
                component: () => {
                    return '<h1>关于页面</h1>'
                }
            }
        ]

        let routerView = document.getElementById('root')

        window.addEventListener('DOMContentLoaded', () => {
            onload()
        })

        function onload() {  //渲染对应的组件
            let linkList = document.querySelectorAll('a[href]')
            linkList.forEach(el => {
                el.addEventListener('click', (e) => {
                    e.preventDefault()  //阻止默认行为
                    history.pushState(null, '', el.getAttribute('href'))
                    renderView(location.pathname)
                    
                })
            })
        }

    </script>

</body>

</html>